// pages/mine/mine.js

const {getMyFamily} = require("../../services/myFamilyReq");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        isLogin: true,
        volunteerCredits: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (wx.getStorageSync("token")) {
            // 用当前的token 值再次获取自己的值,建立在有家庭存在的基础上
            let volunteerInfo = wx.getStorageSync('volunteerInfo')

            getMyFamily(res => {
                let familyInfo = res.data.data.member;
                wx.setStorageSync('familyInfo', res.data.data);
                for (const familyInfoKey in familyInfo) {
                    if (volunteerInfo.volunteerId === familyInfo?.[familyInfoKey]?.volunteerId) {
                        wx.setStorageSync("volunteerInfo", familyInfo[familyInfoKey]);
                        break;
                    }
                }
            });
            let volunteerCredits = volunteerInfo.volunteerCredits
            this.setData({
                isLogin: true,
                volunteerCredits
            });
        } else {
            this.setData({
                isLogin: false,
            });
        }

    },


    // 跳转我的信息
    handleGoMyInfo() {
        wx.navigateTo({
            url: "../myInfo/myInfo",
        });
    },

    // 跳转我的订单
    handleGoMyOrder() {
        wx.navigateTo({
            url: "../myOrder/myOrder",
        });
    },

    // 跳转我的课程
    handleGoMyGift() {
        wx.navigateTo({
            url: "../myGift/myGift",
        });
    },
    //跳转到我的课程
    handleGoMyCourse() {
        wx.navigateTo({
            url: "../myCourse/myCourse",
        });
    },
    // 跳转我参加的活动
    handleGoMyActivity() {
        wx.navigateTo({
            url: "../myActivity/myActivity",
        });
    }, handleGoMyActivityContent() {
        wx.navigateTo({
            url: "../MyActivityContent/MyActivityContent",
        });
    },

    // 跳转我的社团
    handleGoMyCommunity() {
        wx.navigateTo({
            url: "../myCommunity/myCommunity",
        });
    },

    // 跳转我的家庭
    handleGoMyFamily() {
        wx.navigateTo({
            url: "../myFamily/myFamily",
        });
    },
    // 跳转登陆界面
    handleGoLogin() {
        wx.navigateTo({
            url: "../login/login",
        });
    },
    // 退出登录
    handleLogout() {
        wx.removeStorageSync("volunteerInfo");
        wx.removeStorageSync("token");
        this.setData({
            isLogin: false,
        });
        wx.switchTab({
            url: "../index/index",
        });
    },
});
